package com.shazam.test.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.shazam.test.R;
import com.shazam.test.adapter.MenuItemAdapter;
import com.shazam.test.bean.RowItem;
import com.shazam.test.parser.XMLParser;
import com.shazam.test.util.ConnectionDetector;
import com.shazam.test.util.Consts;

public class XMLParsingActivity extends ListActivity {

	private ArrayList<RowItem> rowItems;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		if (!new ConnectionDetector(this).isConnectingToInternet()) {
			showAlertDialog(this,
					getResources().getString(R.string.noconnection_title),
					getResources().getString(R.string.noconnection_desc), false);
		} else {
			setContentView(R.layout.main);
			new XmlParserAsyncTask().execute();

			// selecting single ListView item
			ListView lv = getListView();

			lv.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {

					// Starting new intent
					Intent in = new Intent(getApplicationContext(),
							SingleMenuItemActivity.class);
					in.putExtra(Consts.KEY_LINK, rowItems.get(position)
							.getLink());
					startActivity(in);

				}
			});
		}

	}

	private class XmlParserAsyncTask extends
			AsyncTask<Void, Void, List<HashMap<String, String>>> {

		@Override
		protected List<HashMap<String, String>> doInBackground(
				Void... params) {

			XMLParser parser = new XMLParser();
			String xml = parser.getXmlFromUrl(Consts.URL); // getting XML
			if (xml != null) {
				Document doc = parser.getDomElement(xml); // getting DOM element

				NodeList nl = doc.getElementsByTagName(Consts.KEY_ITEM);
				// looping through all item nodes <item>
				rowItems = new ArrayList<RowItem>();
				for (int i = 0; i < nl.getLength(); i++) {
					Element e = (Element) nl.item(i);
					RowItem item = new RowItem(R.drawable.feed_icon,
							parser.getValue(e, Consts.KEY_TITLE),
							parser.getValue(e, Consts.KEY_TRACKARTIST),
							parser.getValue(e, Consts.KEY_LINK));
					rowItems.add(item);
				}
			}
			return null;
		}

		@Override
		protected void onPostExecute(List<HashMap<String, String>> result) {

			MenuItemAdapter myAdapter = new MenuItemAdapter(
					XMLParsingActivity.this, rowItems);
			setListAdapter(myAdapter);

			super.onPostExecute(result);
		}

	}

	/**
	 * Function to display simple Alert Dialog
	 * 
	 * @param context
	 *            - application context
	 * @param title
	 *            - alert dialog title
	 * @param message
	 *            - alert message
	 * @param status
	 *            - success/failure (used to set icon)
	 * */
	public void showAlertDialog(Context context, String title, String message,
			Boolean status) {
		AlertDialog alertDialog = new AlertDialog.Builder(context).create();

		// Setting Dialog Title
		alertDialog.setTitle(title);

		// Setting Dialog Message
		alertDialog.setMessage(message);

		// Showing Alert Message
		alertDialog.show();
	}
}